package com.adkun.markdown.markdown;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 小工具
 * @author adkun
 */
public class MdTools {

    /**
     * 将字符串按\n分割
     * @param html
     * @return
     */
    public static List<String> splitStr(String html) {
        String[] strs = html.split("\n");
        List<String> list = new ArrayList<>(strs.length + 2);
        list.add("\n");
        for (int i = 0; i < strs.length; i++) {
            if (i == 0 && "".equals(strs[i])) {
                continue;
            }
            list.add(strs[i]);
        }
        list.add("\n");
        return list;
    }

    public static String parseToHtmlInline(String line) {
        for (int i = 0; i < line.length(); i++) {
            // 图片
            if (i < line.length() - 4 && line.charAt(i) == '!' && line.charAt(i + 1) == '[') {
                int index1 = line.indexOf(']', i + 1);
                if (index1 != -1 && line.charAt(index1 + 1) == '(' && line.indexOf(')', index1 + 2) != -1) {
                    int index2 = line.indexOf(')', index1 + 2);
                    String picName = line.substring(i + 2, index1);
                    String picPath = line.substring(index1 + 2, index2);
                    line = line.replace(line.substring(i, index2 + 1), "<img alt='" + picName + "' src='" + picPath + "' />");
                }
            }
            // 链接
            if (i < line.length() - 3 && ((i > 0 && line.charAt(i) == '[' && line.charAt(i - 1) != '!') || (line.charAt(0) == '['))) {
                int index1 = line.indexOf(']', i + 1);
                if (index1 + 1 < line.length() && index1 != -1 && line.charAt(index1 + 1) == '(' && line.indexOf(')', index1 + 2) != -1) {
                    int index2 = line.indexOf(')', index1 + 2);
                    String linkName = line.substring(i + 1, index1);
                    String linkPath = line.substring(index1 + 2, index2);
                    line = line.replace(line.substring(i, index2 + 1), "<a href='" + linkPath + "'> " + linkName + "</a>");
                }
            }
            // 行内引用
            if (i < line.length() - 1 && line.charAt(i) == '`' && line.charAt(i + 1) != '`') {
                int index = line.indexOf('`', i + 1);
                if (index != -1) {
                    String quoteName = line.substring(i + 1, index);
                    line = line.replace(line.substring(i, index + 1), "<code>" + quoteName + "</code>");
                }
            }
            // 粗体
            if (i < line.length() - 2 && line.charAt(i) == '*' && line.charAt(i + 1) == '*') {
                if (i - 6 > -1 && "<code>".equals(line.substring(i - 6, i))) {
                    // 说明有行内引用
                    continue;
                }
                int index = line.indexOf("**", i);
                line = line.replaceFirst("\\*\\*", "<strong>");
                i = i + 8;
                index = line.indexOf("**", i);
                if (index != -1) {
                    String quoteName = line.substring(i, index);
                    line = line.replaceFirst(quoteName + "\\*\\*", quoteName + "</strong>");
                }
            }
            // 斜体
            if (i < line.length() - 2 && line.charAt(i) == '*' && line.charAt(i + 1) != '*') {
                if (i - 6 > -1 && "<code>".equals(line.substring(i - 6, i))) {
                    // 说明有行内引用
                    continue;
                } else if (i - 7 > -1 && "<code>".equals(line.substring(i - 7, i))) {
                    // 说明有行内引用
                    continue;
                }
                int index = line.indexOf('*', i + 1);
                if (index != -1 && line.charAt(index + 1) != '*') {
                    String quoteName = line.substring(i + 1, index);
                    line = line.replace(line.substring(i, index + 1), "<i>" + quoteName + "</i>");
                }
            }
        }
        return line;
    }
}
